<?php
/**
 * Created by PhpStorm.
 * User: Digvijay
 * Date: 4/6/14
 * Time: 6:47 PM
 */

class Charts extends MY_Controller {

    public function salesVsPurchase() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $from = trim($this->input->post('dateFrom'));
        $to = trim($this->input->post('dateTo'));

        if ($from == null || $from == "" || $to == null || $to == "") {
            $to = new DateTime();
            $to = $to->format("d-m-Y");
            $from = new DateTime();
            $from = "1-" . $from->format("m-Y");
        }

        $solidPetroStock = $this->getDataFor("Solidpetrostock", $from, $to, "ASC");
        $response = array();
        $header = array('Date', 'Sales', 'Purchase');
        array_push($response, $header);
        foreach ($solidPetroStock as $sp) {
            $day = array($sp->getDate()->format('d-M-y'), (int)$sp->getSaleAndConsumption(), (int)$sp->getPurchaseAndProduction());
            array_push($response, $day);
        }
        echo json_encode($response);
    }

    public function salesVsGrade() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $from = trim($this->input->post('dateFrom'));
        $to = trim($this->input->post('dateTo'));

        if ($from == null || $from == "" || $to == null || $to == "") {
            $to = new DateTime();
            $to = $to->format("d-m-Y");
            $from = new DateTime();
            $from = "1-" . $from->format("m-Y");
        }

        $query = $this->em->createQuery("SELECT g.name , sum(s.saleAndConsumption) FROM Solidpetrostock s  JOIN s.grade g WHERE s.date >= :from AND s.date <= :to GROUP BY g.name");
        $query->setParameter("from", $from);
        $query->setParameter("to", $to);

        $result = array();
        array_push($result, array("Grade", "Total"));
        foreach ($query->getResult() as $arr) {
            $subArray = array($arr['name'], (int)$arr[1]);
            array_push($result, $subArray);
        }
        echo json_encode($result);
    }

    public function purchaseVsGrade() {
        if (!$this->input->is_ajax_request()) {
            show_404();
            return;
        }
        $from = trim($this->input->post('dateFrom'));
        $to = trim($this->input->post('dateTo'));

        if ($from == null || $from == "" || $to == null || $to == "") {
            $to = new DateTime();
            $to = $to->format("d-m-Y");
            $from = new DateTime();
            $from = "1-" . $from->format("m-Y");
        }

        $query = $this->em->createQuery("SELECT g.name , sum(s.purchaseAndProduction) FROM Solidpetrostock s  JOIN s.grade g WHERE s.date >= :from AND s.date <= :to GROUP BY g.name");
        $query->setParameter("from", $from);
        $query->setParameter("to", $to);
        $result = array();
        array_push($result, array("Grade", "Total"));
        foreach ($query->getResult() as $arr) {
            $subArray = array($arr['name'], (int)$arr[1]);
            array_push($result, $subArray);
        }
        echo json_encode($result);
    }
} 